# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: admin_view.py

1. 创建北京、上海 2 所学校  ---> 管理员创建学校
2. 创建linux , python , go 3个课程 ， linux\py 在北京开， go 在上海开
3. 课程包含，周期，价格，通过学校创建课程
4. 创建讲师
5. 创建学员时，选择学校，关联班级
6.3 管理视图，创建讲师， 创建班级，创建课程
视图层
Author: kij
Create Date: 2021/2/22
-------------------------------------------------
"""
import hashlib

from interface import admin_interface, common_interface
from lib import common
logon_state = {"user": None}


def register():
	print("注册".center(50, "="))
	while True:
		user_name = input("请输入用户名:").strip()
		user_password = input("请输入用户密码:").strip()
		user_password2 = input("再次输入用户密码").strip()
		if user_password!=user_password2:
			print("两次密码不一样")
			continue
		# 未做加密 未加盐
		m = hashlib.md5(user_password.encode('utf-8'))
		m.update(user_password.encode('utf-8'))
		# m.hexdigest()
		is_success, msg = admin_interface.register_interface(user_name, m.hexdigest())
		if is_success:
			print(msg)
			break
		else:
			print(msg)


def logon():
	print("登录".center(50, "="))
	user_name = input("请输入用户名:").strip()
	user_password = input("请输入用户密码:").strip()

	m = hashlib.md5(user_password.encode('utf-8'))
	m.update(user_password.encode('utf-8'))

	exists, data = common_interface.all_logon(user_name, m.hexdigest(), user_type='admin')
	if exists:
		print("登录成功")
		logon_state['user'] = user_name
	else:
		print("登录失败")

@common.logon('admin')
def create_school():
	print("创建学校".center(50, "="))
	while True:
		school_name = input("请输入学校名:").strip()
		school_address = input("请输入学校地址:").strip()
		is_success, msg = admin_interface.create_school_interface(school_name, school_address, logon_state['user'])
		if is_success:
			print(msg)
			break
		else:
			print(msg)


# 创建课程
@common.logon('admin')
def create_lesson():
	print("创建课程".center(50, "="))
	while True:
		# 拿到学校信息然后显示让用户选择
		school_list = common_interface.get_all_school(logon_state['user'])
		for item in range(len(school_list)):
			print("%s %s" %(item, school_list[item].user_name))
		school_num = input("请输入选择的学校:").strip()
		lesson_name = input("请输入课程名称:").strip()
		lesson_time = input("请输入课程周期:").strip()
		lesson_price = input("请输入课程价格:").strip()

		school_name = school_list[int(school_num)].user_name
		is_success, msg = admin_interface.create_lesson_interface(lesson_name, lesson_time, lesson_price, school_name)
		if is_success:
			print(msg)
			break
		else:
			print(msg)

# 创建老师
@common.logon('admin')
def create_teacher():
	print("创建老师".center(50, "="))
	while True:
		teacher_name = input("请输入老师姓名:").strip()
		is_success, msg = admin_interface.create_teacher_interface(teacher_name, logon_state['user'])
		if is_success:
			print(msg)
			break
		else:
			print(msg)



func_dict = {
	"1": ["注册", register],
	"2": ["登录", logon],
	"3": ["创建学校", create_school],
	"4": ["创建课程", create_lesson],
	"5": ["创建老师", create_teacher],
}


def show_index():
	while True:
		print("管理视图".center(50, "="))
		for item in func_dict:
			print("%s %s".center(50, " ") % (item, func_dict[item][0]))
		choice = input("输入选项:").strip()
		if not choice.isdecimal():
			continue

		if choice not in ["1", "2", "3", "4", "5"]:
			continue
		func_dict[choice][1]()
